package cn.taobaoke.restful.controller.agent;

import cn.taobaoke.restful.controller.BaseController;
import cn.taobaoke.restful.interceptor.Auth;
import cn.taobaoke.restful.service.StatisticsService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author yangzuo
 * @since 2017-03-27
 */
@RestController
@RequestMapping("/{domain}/{domainId}/api/agents")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StatisticsController extends BaseController {

    @Auth(type = Auth.AuthType.AGENT)
    @GetMapping("/{agentId}/statics/today")
    public Result staticsToady(@PathVariable String domain, @PathVariable Long domainId, @PathVariable Long agentId) {
        logger.debug("GET /{}/api/agents/{}/statics/today", domain, agentId);

        Map map = statisticsService.staticsAgentToday(agentId, domain, domainId);
        return wrapObj(map);
    }

    @Auth(type = Auth.AuthType.AGENT)
    @GetMapping("/{agentId}/subAgents/income")
    public Result getAdminSubAgentIncome(@PathVariable String domain, @PathVariable Long domainId, @PathVariable Long agentId, String keyword, String start, String end, Integer page, Integer size) {
        logger.debug("GET /{}/api/agents/{}/subAgents/income?keyword={}&start={}&end={}&page={}&size={}", domain, agentId, keyword, start, end, page, size);

        start = start == null ? DateTime.now().plusDays(-7).toString("yyyy-MM-dd") : start;
        end = end == null ? DateTime.now().toString("yyyy-MM-dd") : end;

        keyword = keyword == null ? "" : keyword;

        page = page == null || page < 1 ? 1 : page;
        size = size == null || size < 0 ? 20 : size;

        List<Map> results = statisticsService.queryAgentSubAgentIncome(keyword, agentId, start, end, page - 1, size, domain, domainId);
        Long total = statisticsService.countSubAgent(keyword, agentId, domainId);
        return wrapPage(page, size, total, results);
    }

    @Auth(type = Auth.AuthType.AGENT)
    @GetMapping("/{agentId}/orderAndIncome")
    public Result getAgentOrderAndIncome(@PathVariable String domain, @PathVariable Long agentId, String start, String end) {
        logger.debug("GET /{}/api/agents/{}/orderAndIncome?start={}&end={}", domain, agentId, start, end);

        List<Map> results = statisticsService.queryAgentOrderCountAndIncome(agentId, start, end, domain);
        return wrapObj(results);
    }

    @Auth(type = Auth.AuthType.AGENT)
    @GetMapping("/{agentId}/agents")
    public Result getAgentAgents(@PathVariable String domain, @PathVariable Long domainId, @PathVariable Long agentId, String start, String end) {
        logger.debug("GET /{}/api/agents/{}/agents?start={}&end={}", domain, agentId, start, end);

        List<Map> results = statisticsService.queryAgentSubAgentCount(agentId, start, end, domainId);
        return wrapObj(results);
    }

    @Auth(type = Auth.AuthType.AGENT)
    @GetMapping("/{agentId}/members")
    public Result getAgentMembers(@PathVariable String domain, @PathVariable Long domainId, @PathVariable Long agentId, String start, String end) {
        logger.debug("GET /{}/api/agents/{}/members?start={}&end={}", domain, agentId, start, end);

        List<Map> results = statisticsService.queryAgentMemberCount(agentId, start, end, domainId);
        return wrapObj(results);
    }

    private final @NonNull
    StatisticsService statisticsService;

    private static Logger logger = LogManager.getLogger(StatisticsController.class);
}
